using System;
using System.Timers;
namespace cs_elbot
{
    public class Storage
    {
        // OnGotNewStorageList
        public delegate void GotNewStorageListEventHandler(object sender, GotNewStorageListEventArgs e);
        public event GotNewStorageListEventHandler GotNewStorageList;

        public class GotNewStorageListEventArgs : EventArgs
        {
            public System.Collections.ArrayList TheStorage;
            public GotNewStorageListEventArgs(System.Collections.ArrayList TheStorage)
            {
                this.TheStorage = TheStorage;
            }
        }

        protected void OnGotNewStorageList(GotNewStorageListEventArgs e)
        {
            if (GotNewStorageList != null)
                GotNewStorageList(this, e);
        }
        private System.Collections.ArrayList TheStorage = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList());

        public struct StorageItem
        {
            public byte pos;
            public int imageid;
            public UInt32 quantity;
            public string name;
            public uint category_num;
            public int knownItemsID;
            public UInt32 reservedQuantity;
        }

        public struct StorageCategory
        {
            public int num;
            public string name;
        }

        private TCPWrapper TheTCPWrapper;
        private Logger TheLogger;
        private MySqlManager TheMySqlManager;

        public System.Collections.ArrayList TheStorageCategories = new System.Collections.ArrayList();

        public Storage(TCPWrapper MyTCPWrapper, Logger MyLogger, MySqlManager MyMySqlManager)
        {
            this.TheTCPWrapper = MyTCPWrapper;
            //this.TheTCPWrapper.GotCommand += new TCPWrapper.GotCommandEventHandler(OnGotCommand);
            this.TheLogger = MyLogger;
            this.TheMySqlManager = MyMySqlManager;
        }

        public System.Collections.ArrayList GetStorageList()
        {
            return (System.Collections.ArrayList)TheStorage.Clone();
        }

        public void AddItem(StorageItem MyStorageItem)
        {
            TheStorage.Add(MyStorageItem);
            //add the row to the database
            return;
        }
        public void ClearList()
        {
            TheStorage.Clear();
            //delete the stroage in the database
            TheMySqlManager.ClearStorage();
            return;
        }
        public int Count()
        {
            return TheStorage.Count;
        }

        public void UpdateText(string nameText)
        {
            int idx;
            nameText = nameText.Replace((char)10, ' ');
            lock (TheStorage.SyncRoot)
            {
                for (idx = 0; idx < TheStorage.Count; idx++)
                {
                    StorageItem MyStorageItem = (StorageItem)TheStorage[idx];
                    if (MyStorageItem.name == "")
                    {
                        MyStorageItem.name = nameText;
                        TradeHandler.TradeItem MyTradeItem = new TradeHandler.TradeItem();
                        MyTradeItem.name = MyStorageItem.name;
                        MyTradeItem.imageid = MyStorageItem.imageid;
                        MyTradeItem.weight = -1;
                        TradeHandler.TradeItem MyTempTradeItem = new TradeHandler.TradeItem();
                        MyTempTradeItem = MyTradeItem;
                        MyStorageItem.knownItemsID = TheMySqlManager.GetStorageSQLID(MyTempTradeItem);
                        //get the reserved amount
                        MyStorageItem.reservedQuantity = TheMySqlManager.ReservedAmount(MyStorageItem.knownItemsID);
                        TheStorage[idx] = MyStorageItem;
                        TheMySqlManager.InsertStorageItem(MyStorageItem);
                        break;
                    }
                }
            }
        }

        public void ClearCategories()
        {
            TheStorageCategories.Clear();
            return;
        }

        public void AddCategory( string category_name, int category_number)
        {
            string nameText = category_name;
            nameText = nameText.Replace((char)10, ' ');
            StorageCategory MyStorageCategory = new StorageCategory();
            MyStorageCategory.name = category_name.Trim();
            MyStorageCategory.num = category_number;
            TheStorageCategories.Add( MyStorageCategory );
            TheMySqlManager.InsertStorageCategory(MyStorageCategory);
            //insert the categories into the table
            return;
        }
    }
}